﻿using AutoMapper;
using Modules;
using Modules.Attachment;
using Modules.Constructor;
using Modules.Corp;
using Modules.Project;
using Modules.System.Area;
using PmSoft;
using PmSoft.Entity;
using System.ComponentModel.DataAnnotations;
using static Modules.Project.ProjectConstants;

namespace ManagementApi.Models.Project;

public class ReportProjectMonthDto : Modules.ReportProjectMonth
{
    #region 扩展属性

    /// <summary>
    /// 对口部门名称
    /// </summary>
    public string IndustryTypeName { get; set; }

    /// <summary>
    /// 施工总包单位
    /// </summary>
    public string CorpName { get; set; }

    /// <summary>
    /// 建设单位名称
    /// </summary>
    public string ConstructorName { get; set; }

    public string CityName { get; set; }
    public string AreaName { get; set; }

    public DateTime PrjCreateTime { get; set; }

    public string PrjStatusName { get; set; }

    /// <summary>
    /// 投资金额
    /// </summary>
    public decimal ContractAmount { get; set; }

    #endregion 扩展属性
}

public static partial class ReportProjectMonthExceptions
{
    public static ReportProjectMonthDto AsDto(this ReportProjectMonth entity)
    {
        ReportProjectMonthDto dto = new();
        var mapper = ServiceAccessor.GetService<IMapper>();
        mapper.Map(entity, dto);

        #region 扩展属性赋值

        var projectService = ServiceAccessor.GetService<ProjectService>();
        var corpService = ServiceAccessor.GetService<CorpService>();
        var constructorService = ServiceAccessor.GetService<ConstructorService>();

        var project = projectService.GetProjectInfo(entity.ProjectId);
        if (project != null)
        {
            dto.IndustryTypeName = project.IndustryType.GetDescription() ?? string.Empty;
            var areaService = ServiceAccessor.GetService<AreaService>();
            dto.CityName = areaService.Get(project.CityCode)?.Name ?? string.Empty;
            dto.AreaName = areaService.Get(project.AreaCode)?.Name ?? string.Empty;
            dto.ContractAmount = project.ContractAmount ?? 0;
            dto.PrjCreateTime = project.CreateTime;
            dto.PrjStatusName = project.PrjStatus?.GetDescription() ?? string.Empty;
            dto.CorpName = corpService.GetCorpInfo(project.LumpContractorCorpId)?.Name ?? string.Empty;
            dto.ConstructorName = constructorService.GetConstructorInfo(project?.BuildCorpId ?? 0)?.Name ?? string.Empty;
        }
 
        #endregion 扩展属性赋值

        return dto;
    }
}